package com.example.admin.brainwave.ui.activity;

import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.admin.brainwave.R;
import com.example.admin.brainwave.base.BaseActivity;
import com.example.admin.brainwave.utils.ToastUtil;
import com.example.admin.brainwave.utils.languageutils.LanguageType;
import com.example.admin.brainwave.utils.languageutils.MultiLanguageUtil;


/**
 * 设置语言页面
 */
public class SetLanguageActivity extends BaseActivity implements View.OnClickListener {

    private RelativeLayout rl_followsytem;
    private RelativeLayout rl_simplified_chinese;
    private RelativeLayout rl_english;
    private TextView tv_top_title;
    private ImageView iv_top_back;
    private ImageView iv_english;
    private ImageView iv_followsystem;
    private ImageView iv_simplified_chinese;
    private int savedLanguageType;


    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_set_language;
    }

    @Override
    protected void initView() {
        rl_followsytem = findViewById(R.id.rl_followsytem);
        rl_simplified_chinese = findViewById(R.id.rl_simplified_chinese);
        rl_english = findViewById(R.id.rl_english);
        iv_top_back = findViewById(R.id.iv_top_back);
        iv_followsystem = findViewById(R.id.iv_followsystem);
        iv_english = findViewById(R.id.iv_english);
        iv_simplified_chinese = findViewById(R.id.iv_simplified_chinese);
        tv_top_title = findViewById(R.id.tv_top_title);
        tv_top_title.setText(getResources().getString(R.string.setting_language_title));
        iv_top_back.setOnClickListener(this);
        rl_followsytem.setOnClickListener(this);
        rl_simplified_chinese.setOnClickListener(this);
        rl_english.setOnClickListener(this);
        savedLanguageType = MultiLanguageUtil.getInstance().getLanguageType();
        if (savedLanguageType == LanguageType.LANGUAGE_FOLLOW_SYSTEM) {
            setFollowSytemVisible();
        } else if (savedLanguageType == LanguageType.LANGUAGE_EN) {
            setEnglishVisible();
        } else if (savedLanguageType == LanguageType.LANGUAGE_CHINESE_SIMPLIFIED) {
            setSimplifiedVisible();
        } else {
            setSimplifiedVisible();
        }
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        int selectedLanguage = 0;
        switch (id) {
            case R.id.iv_top_back:
                finish();
                break;
            case R.id.rl_followsytem:
                setFollowSytemVisible();
                selectedLanguage = LanguageType.LANGUAGE_FOLLOW_SYSTEM;
                MultiLanguageUtil.getInstance().updateLanguage(selectedLanguage);
                ToastUtil.showLong(this, getResources().getString(R.string.restart_app));
                finish();
                break;
            case R.id.rl_simplified_chinese:
                setSimplifiedVisible();
                selectedLanguage = LanguageType.LANGUAGE_CHINESE_SIMPLIFIED;
                MultiLanguageUtil.getInstance().updateLanguage(selectedLanguage);
                ToastUtil.showLong(this, getResources().getString(R.string.restart_app));
                finish();
                break;
            case R.id.rl_english:
                setEnglishVisible();
                selectedLanguage = LanguageType.LANGUAGE_EN;
                MultiLanguageUtil.getInstance().updateLanguage(selectedLanguage);
                ToastUtil.showLong(this, getResources().getString(R.string.restart_app));
                finish();
                break;
        }
    }

    private void setSimplifiedVisible() {
        iv_followsystem.setVisibility(View.GONE);
        iv_english.setVisibility(View.GONE);
        iv_simplified_chinese.setVisibility(View.VISIBLE);
    }

    private void setEnglishVisible() {
        iv_followsystem.setVisibility(View.GONE);
        iv_english.setVisibility(View.VISIBLE);
        iv_simplified_chinese.setVisibility(View.GONE);
    }


    private void setFollowSytemVisible() {
        iv_followsystem.setVisibility(View.VISIBLE);
        iv_english.setVisibility(View.GONE);
        iv_simplified_chinese.setVisibility(View.GONE);
    }
}
